package com.flyan.game.motafx.component.monster;

import com.almasb.fxgl.core.util.LazyValue;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.physics.HitBox;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.flyan.game.motafx.component.BattleComponent;
import com.flyan.game.motafx.component.character.BraveComponent;
import com.flyan.game.motafx.config.GameConfig;
import com.flyan.game.motafx.core.algorithm.BattleAlgorithm;
import com.flyan.game.motafx.core.algorithm.BattleResult;
import com.flyan.game.motafx.core.algorithm.Battler;
import com.flyan.game.motafx.core.enums.EntityTypeEnum;
import com.flyan.game.motafx.core.util.animate.StillAnimateTable;
import javafx.util.Duration;

/**
 * 怪物组件
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/1/31
 */
public class MonsterComponent extends Component {

    private final BattleComponent battleComponent;

    private AnimatedTexture atView;

    /**
     * 怪物动画
     */
    private LazyValue<StillAnimateTable> animateTable = new LazyValue<>(() -> new StillAnimateTable(
            FXGL.image("materials/enemys.png"), 2,
            GameConfig.GRID_SIZE, GameConfig.GRID_SIZE,
            Duration.seconds(0.75)));

    public MonsterComponent() {
        battleComponent = new BattleComponent();
    }


    @Override
    public void onAdded() {
        /* 战斗组件 */
        entity.addComponent(battleComponent);

        /* 根据地形类型渲染动画 */
        EntityTypeEnum type = (EntityTypeEnum) entity.getType();
        atView = new AnimatedTexture(animateTable.get().get(type.getType()));

        /* 挂载并播放动画 */
        entity.getViewComponent().addChild(atView);
        entity.getBoundingBoxComponent().addHitBox(new HitBox(BoundingShape.box(
                GameConfig.GRID_BBOX_SIZE, GameConfig.GRID_BBOX_SIZE)));
        atView.loop();
    }

}
